<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { addBackup } from '@/api/syssetting';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';

  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    params: Object,
    formType: {
      type: Array,
      default: () => [], // 这里默认值建议返回空数组，而不是空对象
    },
  });
  const { loading, setLoading } = useLoading();
  const isShow = ref(true);
  defineOptions({
    name: 'addBackup',
  });
  const formData = ref();

  const emit = defineEmits(['update:visible', 'refresh']);

  const handleSubmit = async () => {
    if (!(await formRef.value?.checkForm())) {
      try {
        setLoading(true);
        const res = await addBackup(formRef.value?.formModel);
        Message.success(res.msg);
        emit('update:visible', false);
        setLoading(false);
        emit('refresh');
      } catch (error) {
        setLoading(false);
      }
    } else {
      Message.error('请正确填写表单！');
    }
  };
</script>

<template>
  <a-modal
    title="新增备份事件"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <pro-form
      v-if="isShow"
      ref="formRef"
      hiddenBtn
      :form-json="formType"
      :form-data="formData"
      @formModelChange="handleFormModelChange"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
</style>
